package KthSmallestElementinaSortedMatrix;

import java.util.Stack;

public class Solution {
    public int kthSmallest(int[][] matrix, int k) {
        int[] choosed=new int[matrix.length];
        int result = 0,t;
        while(k>0){
        	result=Integer.MAX_VALUE; t = 0;
        	for (int i=0;i<matrix.length;i++)
        		if (choosed[i]<matrix.length)
        		if (matrix[i][choosed[i]]<result){
        			result=matrix[i][choosed[i]];
        			t=i;
        		}
        	k--;
        	choosed[t]++; 
        }
        return result;
    }
    public static void main(String args[]) {
    	Solution so=new Solution();
    	int[][] matrix={{ 1,  5,  9}, {10, 11, 13},{12, 13, 15}};
    	System.out.println(so.kthSmallest(matrix, 8));
		
	}
}
